<?
require 'global.php';

$gee=$product;
$url='products';

$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"];
$cateid=2; if(isset($_GET["cateid"]))if(is_numeric($_GET["cateid"]))$cateid=$_GET["cateid"];
$sort=1; if(isset($_GET["sort"]))if(is_numeric($_GET["sort"]))$sort=$_GET["sort"];

if(!isset($arr_product_category[$cateid])) { header('Location:404.html'); exit; }

$tem_cate=$arr_product_category[$cateid];
$cate_name=$arr_product_category[$cateid]['name_'.$lang];
$cate_parents=get_level_parents($tree_product_category,$cateid);
$cate_children=get_level_children($tree_product_category,$cateid);

$has_children=count($cate_children);

// 分类级别 cate level
$cate_level=count($cate_parents);
// 去除自己并反转
unset($cate_parents[0]);
$cate_parents = array_reverse($cate_parents);
//pre($cate_parents);

if($has_children){ // 非最终级别 
	$arr_in=array();
	foreach($cate_children as $val) $arr_in[]= $val['id'];
	//pre($arr_in); exit;
	$gee->rows=16;
	$where=array();
	$where[]=array('name'=>'is_enabled','val'=>1);
	$where[]=array('name'=>'category_id','val'=>$arr_in,'opn'=>'in');
	
	switch ($sort) {
    case 1:
        $order_by='id';
		$order_direction='desc';
        break;
    case 2:
        $order_by='price';
		$order_direction='asc';
        break;
    case 3:
        $order_by='price';
		$order_direction='desc';
        break;
	}

	$pages=$gee->count_pages($where);
	$info=$gee->get_page($page,$where,$order_by,$order_direction);
	
	// seo
	$title=$cate_name.' - '.$config['SITE_TITLE_'.LANG];
	$kwd=$title;
	$dpn=$tem_cate['description_'.$lang];
}
else {
	$gee->rows=12;
	$where=array();
	$where[]=array('name'=>'is_enabled','val'=>1);
	$where[]=array('name'=>'category_id','val'=>$cateid);
	switch ($sort) {
    case 1:
        $order_by='id';
		$order_direction='desc';
        break;
    case 2:
        $order_by='price';
		$order_direction='asc';
        break;
    case 3:
        $order_by='price';
		$order_direction='desc';
        break;
	}
	$pages=$gee->count_pages($where);
	$info=$gee->get_page($page,$where,$order_by,$order_direction);
	
	// seo
	$title=$cate_name.' - '.$config['SITE_TITLE_'.LANG];
	$kwd=$title;
	$dpn=$tem_cate['description_'.$lang];
}

// 热销产品 hot_products
$tem = $product->get_rows(32,'is_enabled',1,'sold_quantity','desc');
shuffle($tem);
$arr_hot=array();
$k=0;
foreach($tem as $row) { $arr_hot[]=$row; $k++; if($k==MAX_HOT_PRODUCT_SHOWN_SIDE) break; }

//  历史记录 history
$history=get_history();
$has_history=count($history);
$where_history=array();
$where_history[]=array('name'=>'is_enabled','val'=>1);
$where_history[]=array('name'=>'id','val'=>$history,'opn'=>'in');
$tem = $product->get_rows_advanced(32,$where_history);
shuffle($tem);
$arr_history=array();
$k=0;
foreach($tem as $row) { $arr_history[]=$row; $k++; if($k==MAX_RECENTLY_VIEWED_PRODUCT_SHOWN_SIDE) break; }

// SEO 设置 seo tags
$title=WEB_ALL_PRODUCTS;
$kwd=$title;
$dpn=$kwd;

// view 加载视图
require VIEW.$url.'.php';

// end of file
